package Sort;

/**
 * Created by sunwei on 2017/9/28
 * Time:14:32
 */
public class DataSort {
    Data a[];
    int nElems;

    public DataSort(int maxSize) {
        a=new Data[maxSize];
        nElems=0;
    }
    //添加数据
    public void addData(int data){
        a[nElems] = new Data(data);
        nElems++;
    }
    //显示数据
    public void deployData(){
        for(int i = 0; i <nElems ; i++) {  
            a[i].deployData();
        }   
    }
    //冒泡排序
    public void BubbleSort(){
        int in,out;
        for(out = nElems-1;out>0 ;out--) {
            for(in = 0; in<out ;in++) {
                if (a[in].getA()>a[in+1].getA()) {
                      Data temp;
                      temp = a[in];
                      a[in] = a[in+1];
                      a[in+1] = temp;
                }
            }
        }  
    }
    //插入排序、
    public void InsertionSort(){
        int in,out;
        for(out =1;out<nElems;out++) {
            Data temp = a[out];
            in=out;
            while (in>0&&a[in-1].getA()>temp.getA()){
                a[in] = a[in-1];
                --in;
            }
            a[in] = temp;
        }
    }
    // 选择排序
    public void selectionSort(){
        int in,out,min;
        for(out = 0;out<nElems-1 ;out++) {
            min = out;
            for(in = out+1;in<nElems;in++) {
                if (a[min].getA()>a[in].getA()) {
                      min = in;
                }
            }
            Data temp;
            temp = a[min];
            a[min] = a[out];
            a[out] = temp;
        }
    }
}
